package com.usefullc.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.enums.YesNoEnum;
import com.usefullc.common.user.OnlineUser;
import com.usefullc.common.user.OnlineUserManager;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.service.AbstractBizService;
import com.usefullc.user.domain.*;
import com.usefullc.user.enums.UserStatusEnum;
import com.usefullc.user.mapper.EmployeeMapper;
import com.usefullc.user.param.EmployeeEditParam;
import com.usefullc.user.query.EmployeeQuery;
import com.usefullc.user.vo.EmployeeVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Sampson
 * @date: 2022-01-09 09:58
 */
@Service
public class EmployeeService extends AbstractBizService<EmployeeMapper,Employee,EmployeeQuery> {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private PositionService positionService;


    @Override
    protected QueryWrapper<Employee> getQueryWrapper(EmployeeQuery query) {
        QueryWrapper<Employee> queryWrapper = super.getQueryWrapper(query);
        OnlineUser loginUser = OnlineUserManager.getLoginUser();
        if(loginUser != null && loginUser.getTenantId() != 0L){
            query.setTenantId(loginUser.getTenantId());
        }
        return queryWrapper;
    }

    public Pager<EmployeeVo> findVoByPage(EmployeeQuery query){

        Pager<Employee> pager = this.findByPage(query);
        Pager<EmployeeVo> newPager = new Pager<>(pager);
        List<Employee> dataList = pager.getDataList();
        List<EmployeeVo> newDataList = new ArrayList<>();
        newPager.setDataList(newDataList);
        for (Employee employee : dataList) {
            User user = userService.findById(employee.getUserId());
            EmployeeVo employeeVo = BeanUtils.beanCopy(user, EmployeeVo.class);
            employeeVo.setId(employee.getId());
            employeeVo.setJobNo(employee.getJobNo());
            employeeVo.setPhone(employee.getPhone());
            employeeVo.setUserId(user.getId());


            if(user.getDeptId() != null){
                Dept dept = deptService.findById(user.getDeptId());
                employeeVo.setDept(dept);
            }
            if(user.getPositionId() != null){
                Position position = positionService.findById(user.getPositionId());
                employeeVo.setPosition(position);
            }
            List<Role> roleList = userService.getBaseMapper().findRoleListByUserId(user.getId());
            if(CollectionUtils.isNotEmpty(roleList)){
                List<Long> roleIds = roleList.stream().map(data -> data.getId()).collect(Collectors.toList());
                employeeVo.setRoleIds(roleIds);
            }
            newDataList.add(employeeVo);
        }
        return newPager;
    }


    @Transactional
    public void saveEmployee(EmployeeEditParam param){
        User user = BeanUtils.beanCopy(param, User.class);
        user.setId(param.getUserId());
//        user.setType(UserTypeEnum.ADMIN.getCode());
        user.setBind(YesNoEnum.YES.getCode());

//        if(param.getPositionId() != null){
//            Position position = new Position();
//            position.setId(param.getPositionId());
//            user.setPosition(position);
//        }
//
//        if(param.getDeptId() != null){
//            Dept dept = new Dept();
//            dept.setId(param.getDeptId());
//            user.setDept(dept);
//        }
        user.setStatus(UserStatusEnum.PASS.getCode());
        userService.saveUser(user);

        if (param.getId() != null) {
            Employee oldBean = this.findById(param.getId());
            Long userId = oldBean.getUserId();
            if(userId != null && param.getUserId() != userId.longValue()){  //两个用户不一样，解绑老用户
                User oldUser = userService.findById(userId);
                oldUser.setPositionId(null);
                oldUser.setDeptId(null);
                oldUser.setBind(YesNoEnum.NO.getCode());
                userService.update(oldUser);
                //todo 清空该用户在线登录信息
            }
        }


        Employee employee = new Employee();
        employee.setId(param.getId());
        employee.setJobNo(param.getJobNo());
        employee.setName(param.getUserName());
        employee.setPhone(param.getPhone());
        employee.setUserId(param.getUserId());
        this.save(employee);

//        //先删除
//        userService.getBaseMapper().deleteUserRoleByUserId(user.getId());
//
//        List<Long> roleIds = param.getRoleIds();
//        if (CollectionUtils.isNotEmpty(roleIds)) {
//            List<UserRoleParam> userRoleParams = new ArrayList<>();
//            for (Long roleId : roleIds) {
//                UserRoleParam userRoleParam = new UserRoleParam();
//                userRoleParam.setRoleId(roleId);
//                userRoleParam.setUserId(user.getId());
//                userRoleParams.add(userRoleParam);
//            }
//            userService.getBaseMapper().insertUserRoleBatch(userRoleParams);
//        }

    }





}
